/*
 * @(#)XfslEndCondition.java        1.0 2000/05/09
 *
 * This file is part of Xfuzzy 3.0, a design environment for fuzzy logic
 * based systems.
 *
 * (c) 2000 IMSE-CNM. The authors may be contacted by the email address:
 *                    xfuzzy-team@imse.cnm.es
 *
 * Xfuzzy is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation.
 *
 * Xfuzzy is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
//CONDICIONES DE FINALIZACION			//
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//

package xfuzzy.xfsl.model;

public class XfslEndCondition implements Cloneable {

	//----------------------------------------------------------------------------//
	//                            COSTANTES P�BLICAS                              //
	//----------------------------------------------------------------------------//

	public static final int EPOCH = 0;
	public static final int TRN_ERROR = 1;
	public static final int TRN_RMSE = 2;
	public static final int TRN_MXAE = 3;
	public static final int TRN_VAR = 4;
	public static final int TEST_ERROR = 5;
	public static final int TEST_RMSE = 6;
	public static final int TEST_MXAE = 7;
	public static final int TEST_VAR = 8;

	//----------------------------------------------------------------------------//
	//                            COSTANTES PRIVADAS                              //
	//----------------------------------------------------------------------------//

	/**
	 * Nombre de las condiciones de t�rmino
	 */
	private static final String limitname[] =
	{ "epoch", "training_error", "training_RMSE", "training_MXAE",
		"training_variation", "test_error", "test_RMSE", "test_MXAE",
	"test_variation" };

	//----------------------------------------------------------------------------//
	//                            MIEMBROS PRIVADOS                               //
	//----------------------------------------------------------------------------//

	/**
	 * Valores de los l�mites
	 */
	private double limit[] = new double[9];

	//----------------------------------------------------------------------------//
	//                                CONSTRUCTOR                                 //
	//----------------------------------------------------------------------------//

	/**
	 * Constructor
	 */
	public XfslEndCondition() {
		for(int i=0; i<limit.length; i++) limit[i] = -1;
	}

	//----------------------------------------------------------------------------//
	//                             M�TODOS P�BLICOS                               //
	//----------------------------------------------------------------------------//

	/**
	 * Duplicado del objeto
	 */
	public Object clone() {
		try { return super.clone(); }
		catch(CloneNotSupportedException e) { return null; }
	}

	/**
	 * Verifica que existe alguna condici�n fijada
	 */
	public boolean isOn() {
		for(int i=0; i<limit.length; i++) if(limit[i] != -1) return true;
		return false;
	}

	/**
	 * Verifica que se ha alcanzado la condici�n de finalizaci�n
	 */
	public boolean isOver(XfslStatus status) {
		if(status.status == XfslStatus.FINISHED) return true;
		if(status.epoch >= limit[0] && limit[0] != -1) return true;
		if(status.trn.error < limit[1] && limit[1] != -1) return true;
		if(status.trn.rmse < limit[2] && limit[2] != -1) return true;
		if(status.trn.mxae < limit[3] && limit[3] != -1) return true;
		if(status.trn.var<limit[4] && status.trn.var>0 && limit[4]!=-1) return true;
		if(!status.testing) return false;
		if(status.tst.error < limit[5] && limit[5] != -1) return true;
		if(status.tst.rmse < limit[6] && limit[6] != -1) return true;
		if(status.tst.mxae < limit[7] && limit[7] != -1) return true;
		if(status.tst.var<limit[8] && status.tst.var>0 && limit[8]!=-1) return true;
		return false;
	}

	/**
	 * Asigna una condici�n de finalizaci�n
	 */
	public void set(String name, double param) {
		for(int i=0; i<limitname.length; i++)
			if(limitname[i].equals(name)) limit[i] = param;
	}

	/**
	 * Asigna una condici�n de finalizaci�n
	 */
	public void setLimit(int index, double value) {
		limit[index] = value;
	}

	/**
	 * Obtiene el valor de una condici�n de t�rmino	
	 */
	public double getLimit(int index) {
		return limit[index];
	}

	/**
	 * Representaci�n en formato XML
	 * @return
	 */
	public String toXML() {
		String eol = System.getProperty("line.separator", "\n");
		String xml = "";
		for(int i=0; i<limit.length; i++) if(limit[i]>=0)  {
			xml += "\t\t<end_condition name=\""+limitname[i]+"\" value=\""+limit[i]+"\">"+eol;
			xml += "\t\t</end_condition>"+eol;
		}
		return xml;
	}

}

